#include "animator.h"


namespace llx {
Animator::Animator(){
    stopwatch();
}

Animator::~Animator(){}

void Animator::animate(){
    if(elapsedSeconds()<1.0/mFramesPerSecond){
        return;
    }

    switch (mAnimation) {
    case FIRST_PERSON:
        firstperson();
        break;

    case ORBIT:
        orbit();
        break;

    case PAN:
        pan();
        break;

    case ROLL:
        roll();
        break;

    case ZOOM:
        zoom();
        break;

    case NONE:
    default:
        return;
        break;
    }

    stopwatch();
}

double Animator::elapsedSeconds(){
    std::chrono::time_point<std::chrono::system_clock>now = std::chrono::system_clock::now();
    std::chrono::duration<double>seconds = now - mTic;
    return seconds.count();
}

void Animator::firstperson(){}

void Animator::orbit(){
    auto c = mInteractor->getCamera();
    if(0==mFrame){
        mFrames = 5*mFramesPerSecond;
        c->setEye(glm::vec3(1.0f));
        c->setUp(-0.0f,-0.0f,1.0f);
        c->setCenter(glm::vec3(0.0f));
        c->cupdate();
        mInteractor->setCamera(c);
    }

    double x = fmod(mFrame*4,mWidth);
    double y = mHeight*0.74;
    mInteractor->setLeftClicked(true);
    mInteractor->setClickPoint(x,y);

    if(++mFrame>=mFrames){
        mInteractor->setLeftClicked(false);
        reset();
    }
}

void Animator::pan(){}

void Animator::roll(){}

void Animator::zoom(){}

void Animator::reset(){
    mAnimation = NONE;
    mFrame = 0;
}

void Animator::setAnimation(AnimationType type){
    mAnimation = type;
}

void Animator::setInteractor(TrackBallInteractor *i){
    mInteractor = i;
}

void Animator::setScreenSize(int w, int h){
    mWidth = w;
    mHeight = h;
}

void Animator::stopwatch(){
    mTic = std::chrono::system_clock::now();
}


} //end namespace llx
